package ssm.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.RandomUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import ssm.model.UserModel;
import ssm.service.IShardingJdbcService;

@Service
public class ShardingJdbcService implements IShardingJdbcService {

    @Resource
    private SqlSession sqlSession;

    public String insert() {
        UserModel um = new UserModel();
        um.setOrderId(RandomUtils.nextInt(10, 10000));
        um.setUserId(RandomUtils.nextInt(10, 10000));
        um.setBusinessId(RandomUtils.nextInt(10, 10000));
        int num = sqlSession.insert(ShardingJdbcService.class.getName() + ".insert", um);
        JSONObject obj = new JSONObject();
        if (num >= 0) {
            obj.put("code", 0);
            obj.put("msg", "成功");
        } else {
            obj.put("code", 1);
            obj.put("msg", "失败");
        }
        return obj.toJSONString();
    }


    public String list() {
        List<Map<String, Object>> list = sqlSession.selectList(ShardingJdbcService.class.getName() + ".list");
        JSONObject obj = new JSONObject();
        obj.put("list", list);
        return obj.toJSONString();
    }

    @Override
    public void delete() {
        UserModel um = new UserModel();
        um.setCtime(new Date());
        sqlSession.delete(ShardingJdbcService.class.getName() + ".delete", um);
    }

}
